<?php
class Registration_Controller extends RegistrationControllerBase {
	
	public function __construct() {
		parent::__construct();
	}
	
	/** This method is called from the form at the end of the registration wizard. */
	public function confirm() {
		$confirmationForm = $this->postToDbValues();
		$registrationIdList = preg_split('/,/', $confirmationForm['registration_ids']);
	
		foreach ($registrationIdList as $id) {
			if (! valid::isId($id)) {
				$this->msg->error('error.invalidId');
				$this->redirect('/user/seminarChoice');
			}
		}
	
		$registrationList = array();
		foreach ($registrationIdList as $id) {
			$registration = $this->dao->getById('registration', $id);
			$registrationList[] = $registration;
			if (! $registration) {
				$this->msg->error('error-recordNotFound');
				$this->redirect('/user/seminarChoice');
			}
		}
		$this->getSeminarService()->confirmRegistration($registrationList, $confirmationForm['org_notes']);
	
		if ($confirmationForm['endUserSubstitution']) {
			$this->getSessionData()->clearUserSpaceData();
			$this->getSessionData()->setUserSubstitute(NULL);
			$this->redirect('/admin/adminSpace');
		}
		$this->redirect('/user/seminarChoice');
	}
	
	public function cancelRegistration() {
		$registrationId = $this->input->post('id');
		$reason = $this->input->post('cancellation_reason');
	
		if (! valid::isId($registrationId)) {
			$this->redirectToList('error.invalidId');
		}
	
		$registration = $this->getSeminarService()->cancelRegistration($registrationId, $reason);
		$this->redirect('/org/orgSeminar/registrationList?id=' . $registration['seminar_id']);
	}
	
	public function edit() {
    	$id = $this->input->get('id');
        
        if (! valid::isId($id)) {
            $this->msg->error('error.invalidId');
            $this->redirect('/admin/seminar/all');
        }

        $registration = $this->getSeminarService()->loadRegistrationDisplayData($id);
        if (! $registration) {
        	$this->msg->error('error-recordNotFound');
            $this->redirect('/admin/seminar/all');
        }

    	$this->editFormData($registration);
    	$this->renderMySubview('registrationEdit');
    }
    
    private function editFormData($registration) {
    	$viewData = $this->getSeminarService()->loadRegistrationEditFormData($registration);
    	$this->mergeViewData($viewData);
    }
    
	public function save() {
    	$formData = $this->postToDbValues();
        $id = $formData['id'];

        // Basic validation
        $validator = new EntityValidator($formData, 'registration');
        $validator->code('batch_code');
        $validator->code('category_code');
        $validator->booleanOrNull('social_discount_applied');
        $validator->integerOrNull('extra_discount');
        $validator->integerOrNull('price_paid');
        if ($formData['extra_discount']) {
        	$validator->required('extra_discount_reason');
        }
        $validator->booleanOrNull('early_discount_applied');
        $validator->booleanOrNull('late_charge_applied');
        
        if ($this->msg->hasErrors()) {
        	$registration = $this->getSeminarService()->loadRegistrationDisplayData($id);
        	$mergedRegistration = array_merge($registration, $formData);
            $this->editFormData($mergedRegistration);
            $this->renderMySubview('orgRegistrationEdit');
            return;
        }
        
        // Database validation
        $registration = $this->dao->getById('registration', $id);
        if (! $registration) {
        	$this->msg->error('error.recordNotFound');
        	$this->redirect('/admin/seminar/all');
        }
        
        $this->getSeminarService()->saveEditedRegistration($registration, $formData, UserRole::$admin);
        
        $this->redirect('/admin/registration/display?id=' . $id);
    }
}
